<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="角色名称" prop="roleName">
      <el-input v-model="dataForm.roleName" placeholder="角色名称"></el-input>
    </el-form-item>
    <el-form-item label="折扣(0-1)" prop="discount">
      <el-input v-model="dataForm.discount" placeholder="折扣(0-1)"></el-input>
    </el-form-item>
    <el-form-item label="更新人员打折权限" prop="updateEmpFlag">
      <el-switch v-model="dataForm.updateEmpFlag" active-value="1" inactive-value="0"></el-switch>
      <!-- <el-radio v-model="dataForm.updateEmpFlag" label="" placeholder="更新人员打折权限"></el-radio> -->
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-prevent-re-click>确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getInfo, addObj, putObj} from '@/api/admin/sysrolediscount'

    export default {
    data () {
    var valueCheck = (rule, value, callback) => {
      if (value > 1) {
        callback(new Error("请输入0-1之间含两位小数的值"));
      } else if (value < 0) {
        callback(new Error("请输入0-1之间含两位小数的值!"));
      } else {
        callback();
      }
    };

      return {
        visible: false,
        dataForm: {
          roleId: 0,
          roleName: '',
          discount: '',
          updateEmpFlag: '0'
        },
        dataRule: {
          roleName: [
            { required: true, message: '角色名称不能为空', trigger: 'blur' }
          ],
          discount: [
            { required: true, message: '折扣(0-1)不能为空', trigger: 'blur' },
            { validator: valueCheck}
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.roleId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.roleId) {
            getInfo(this.dataForm.roleId).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
                putObj(this.dataForm).then(data => {
                    this.$message.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                });
            } else {
                putObj(this.dataForm).then(data => {
                    this.$message.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                })
            }
          }
        })
      }
    }
  }
</script>
